package org.hiro.psi;

import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.codeStyle.CodeStyleManager;
import org.apache.commons.lang.StringUtils;

public class GetMethodBuilder<T extends GetMethodBuilder> extends PsiMethodBuilder<GetMethodBuilder> {
    private PsiClass classToBuild;
    private String fieldName;

    public GetMethodBuilder(PsiElementFactory elementFactory, CodeStyleManager codeStyleManager) {
        super(elementFactory, codeStyleManager);
    }

    @Override
    public PsiMethod build() {
        withMethodText(createMethodText(fieldName));
        return super.build();
    }

    private String createMethodText(String fieldName) {
        String returnType = classToBuild.getContainingFile().getName().split("\\.")[0];
        return "protected " + returnType + " get" + StringUtils.capitalize(fieldName) + "() {" +
                "return (" + returnType + ") values.get(\"" + fieldName + "\");}";
    }

    public T withFieldName(String fieldName) {
        this.fieldName = fieldName;
        return (T) this;
    }

    public T withClassToBuild(PsiClass classToBuild) {
        this.classToBuild = classToBuild;
        return (T) this;
    }
}